package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.UserInfoBean;
import cn.wikiflyer.beikang.bean.BalanceListBean;
import cn.wikiflyer.beikang.bean.BalanceListBean.BalanceBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.utils.WKTimeUtils;
import cn.wk.libs4a.view.WKHeader;
import cn.wk.libs4a.view.list.WKListViewInterface;
import cn.wk.libs4a.view.list.WKListViewUtils;
import cn.wk.libs4a.view.list.WKViewHolder;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.socialize.utils.Log;

/**
 * 累计佣金
 */
public class UserYongJinListAct extends BaseActivity implements
		WKListViewInterface {

	@ViewInject(id = R.id.header)
	private WKHeader header;

	private TextView center_cash;
	private TextView center_balance;
	private TextView center_unclear;

	private WKListViewUtils<BalanceBean, BalanceListBean> listUtils;
	private UserInfoBean mUserBean;

	@Override
	public void getIntentData(Bundle savedInstanceState) {
		super.getIntentData(savedInstanceState);
		mUserBean = (UserInfoBean) getIntent().getSerializableExtra("bean");
	}

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_yongjin);
	}

	@Override
	public void init() {
		header.setLeftImg("提现", R.drawable.user_outmoney_img, false, 12);
		header.setClickListener(null, null, new OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(ctx, UserBalanceCashAct.class));
			}
		});

		View header_view = LayoutInflater.from(ctx).inflate(
				R.layout.bk_yongjin_head, null);
		center_cash = (TextView) header_view.findViewById(R.id.center_cash);
		center_balance = (TextView) header_view
				.findViewById(R.id.center_balance);
		center_unclear = (TextView) header_view
				.findViewById(R.id.center_unclear);

		PullToRefreshListView pList = (PullToRefreshListView) findViewById(R.id.wk_pull_refresh_list);
		listUtils = new WKListViewUtils<BalanceBean, BalanceListBean>() {
		};
		listUtils.init(ctx, this, pList, header_view);
		if (mUserBean != null) {
			if (mUserBean.history_cash.equals("0")) {
				center_cash.setText("0.00");
			} else {
				center_cash.setText("" + mUserBean.history_cash);
			}

			center_unclear.setText("" + mUserBean.amount);

			try {
				float m = Float.valueOf(mUserBean.money);
				float c = Float.valueOf(mUserBean.history_cash);
				// center_balance.setText("" + (m + c));
				if (mUserBean.sales_cash.equals("0")) {
					center_balance.setText("0.00");
				} else {
					center_balance.setText("" + mUserBean.sales_cash);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void setData() {
		listUtils.updateData();
	}

	@Override
	public String getData(boolean... isLocal) {
		return app().net.getBalance(listUtils.page, listUtils.pageCount,
				isLocal);
	}

	// net:http://115.28.41.231/index.php?m=api&c=orders&a=ie_log&page_num=1&rows=10
	@Override
	public void getDataBack(String result, boolean isLocal) {
		Type cvbType = new TypeToken<BKBaseBean<BalanceListBean>>() {
		}.getType();
		Gson gson = new Gson();
		Log.e("wj", "" + result);

		// 使用数据
		try {

			listUtils.bean = gson.fromJson(result, cvbType);
			listUtils.list.addAll(listUtils.bean.data.list);
			listUtils.adapter.notifyDataSetChanged();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public WKViewHolder createViewHolder(View arg1) {
		return new MyViewHolder(arg1);
	}

	@Override
	public int getItemViewId() {
		return R.layout.bk_yongjin_item;
	}

	class MyViewHolder extends WKViewHolder<BalanceBean> {
		TextView timeText;
		TextView proText;
		TextView countText;
		TextView markText;

		public MyViewHolder(View view) {
			timeText = (TextView) view.findViewById(R.id.income_time);
			proText = (TextView) view.findViewById(R.id.income_pro);
			countText = (TextView) view.findViewById(R.id.income_count);
			markText = (TextView) view.findViewById(R.id.income_mark);
		}

		@Override
		public void setData(final BalanceBean t) {
			timeText.setText(t.buyer + "");
			proText.setText(t.level_title + "");
			countText.setText(t.orderno + "");
			markText.setText(t.money + "");
			// markText.setText(WKTimeUtils.formatTime(t.logtime));
		}
	}

}
